﻿using System.Collections.Generic;
using eCommerce.db.DALRespository;
using eCommerce.db.DataRespository;
using eCommerce.core.Helpers;
using System.Web;
using System.Web.Caching;
using System;

namespace eCommerce.db.BORespository
{
    public class CategoryBo
    {
        public static List<CategoryEntity> PaginatedCollection(List<CategoryEntity> categories, int pageIndex, int pageSize)
        {
            var paginatedCollection = new List<CategoryEntity>();

            var windowFloor = pageIndex == 1 ? -1 : ((pageSize * pageIndex) - 1) - pageSize >= 0 ? ((pageSize * pageIndex) - 1) - pageSize : 0;

            var windowCeiling = (pageSize * pageIndex) - 1;

            foreach (var i in categories)
            {
                var a = categories.IndexOf(i);
                var b = a;
                if (categories.IndexOf(i) <= windowCeiling && categories.IndexOf(i) > windowFloor)
                    paginatedCollection.Add(i);
            }

            return paginatedCollection;
        }

        public static void GetListTree(List<CategoryEntity> arrayList, int typeId, int level, int parentId, int isActive)
        {
            var categories = GetListByParentId(typeId, parentId, isActive);

            foreach (var m in categories)
            {
                var prefix = string.Empty;

                for (var i = 0; i < level; i++)
                {
                    prefix += "--- ";
                }

                m.Name = prefix == string.Empty ? string.Format("{0}", m.Name.Trim()) : prefix + m.Name;

                arrayList.Add(m);

                level++;
                GetListTree(arrayList, typeId, level, m.CategoryId, isActive);
                level--;
            }
        }

        public static List<CategoryEntity> GetListByParentId(int typeId, int parentId, int isActive)
        {
            List<CategoryEntity> list = null;
            var cacheName = Utils.GET_CATEGORY_GETLISTBYPARENTID(typeId, parentId, isActive);

            list = Utils.GetFromCacheIIS<List<CategoryEntity>>(cacheName);
            if (list == null)
            {
                list = CategoryDal.GetListByParentId(typeId, parentId, isActive);
                HttpContext.Current.Cache.Insert(cacheName, list, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
            }
            return list;
        }

        public static CategoryEntity GetById(int id)
        {
            CategoryEntity list = null;
            var cacheName = Utils.GET_CATEGORY_GETBYID(id);

            list = Utils.GetFromCacheIIS<CategoryEntity>(cacheName);
            if (list == null)
            {
                list = CategoryDal.GetById(id);
                HttpContext.Current.Cache.Insert(cacheName, list, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
            }
            return list;
        }

        public static CategoryEntity GetByUrl(string categoryName)
        {
            return CategoryDal.GetByUrl(categoryName);
        }     

        public static string GetCategoryName(int id)
        {
            try { return GetById(id).Name; }
            catch { return string.Empty; }
        }

        public static void Save(CategoryEntity o)
        {
            CategoryDal.Save(o);
        }

        public static void SavePriority(int id, int priority)
        {
            CategoryDal.SavePriority(id, priority);
        }

        public static void Delete(int id)
        {
            CategoryDal.Delete(id);
        }
    }
}
